package com.google.code.swingonfire.plotter;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Path2D;

public class ArrowSketch implements Sketch
{

    private final Rectangle bounds;
    private final Path2D path;

    /**
     * Creates an arrow
     * 
     * @param pointerLength the pointer length relative to the pointer height
     * @param tailLength the tail length relative to the pointers height
     * @param tailWidth the tail width relative to the pointers height
     */
    public ArrowSketch(double pointerLength, double tailLength, double tailWidth)
    {
        super();

        pointerLength *= 10;
        tailWidth = (1-tailWidth) * 5;
        tailLength *= 10;
        double center = (tailLength - pointerLength) / 2;

        bounds =
            new Rectangle((int) -Math.ceil(tailLength), -5, (int) (Math.ceil(pointerLength) + Math.ceil(tailLength)),
                10);

        path = new Path2D.Float();
        path.moveTo(pointerLength + center, 0);
        path.lineTo(center, 5);

        if (tailLength > 0)
        {
            path.lineTo(center, 5 - tailWidth);
            path.lineTo(-tailLength + center, 5 - tailWidth);
            path.lineTo(-tailLength + center, -5 + tailWidth);
            path.lineTo(center, -5 + tailWidth);
        }

        path.lineTo(center, -5);
        path.closePath();
    }

    @Override
    public Rectangle getBounds()
    {
        return bounds;
    }

    @Override
    public void draw(Graphics2D g)
    {
        g.fill(path);
    }

}
